package cn.itcast.web.controller.message;

import cn.itcast.domain.message.Message;
import cn.itcast.domain.system.User;
import cn.itcast.service.message.MessageService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/message",name = "用户反馈")
public class MessageController extends BaseController {
    @Reference
    private UserService userService;

    @Reference
    private MessageService messageService;


    @RequestMapping(value = "/message", name = "用户留言")
    public String message() {

        return "message/message";
    }




    @ResponseBody
    @RequestMapping(value = "/smallMessage", name = "小图标留言查看")
    public List<Message> smallMessage() {
        // 用户权限
        User user = userService.findById(getLoginUser().getId());
        Integer degree = user.getDegree();
        List<Message> messageList = null;
        if (degree == 0){
            messageList = messageService.findAllNoRead(0);
            int count = messageList.size();
            request.getSession().setAttribute("count",count);

            System.out.println(JSONObject.toJSONString(request.getSession())+"---------------------------");
            // 查询发给用户的邮件
            for (Message message : messageList) {
                long oldTime = message.getSendTime().getTime();
                long newTime = System.currentTimeMillis();

                String beforeTime = timeJudge(newTime, oldTime);
                message.setBeforeTime(beforeTime);

            }

        }else if(degree == 1){
            messageList = messageService.findByCompanyIdNoRead(getCompanyId(),0);
            // 消息数量
            int count = messageList.size();
            request.getSession().setAttribute("count",count);
            System.out.println("+++++"+JSONObject.toJSONString(request.getSession())+"---------------------------");
            for (Message message : messageList) {

                long oldTime = message.getSendTime().getTime();
                long newTime = System.currentTimeMillis();

                String beforeTime = timeJudge(newTime, oldTime);
                message.setBeforeTime(beforeTime);

            }

        }else{
            // 查询发给用户的邮件
            messageList = messageService.findByMail(getLoginUser().getEmail(), 0);
            int count = messageList.size();
            request.getSession().setAttribute("count",count);
            System.out.println(JSONObject.toJSONString(request.getSession())+"---------------------------");
            for (Message message : messageList) {
                // 设置阅读状态
                message.setReadState(1);
                long oldTime = message.getSendTime().getTime();
                long newTime = System.currentTimeMillis();

                // 进行时间判断
                String beforeTime = timeJudge(newTime, oldTime);
                message.setBeforeTime(beforeTime);
            }
        }
        return messageList;
    }




    public static String timeJudge(long newTime,long oldTime){
        int time1 = 0;

        // 进行时间判断
        if ((newTime - oldTime) / 1000 < 60) {
            time1 = (int) (newTime - oldTime) / 1000;
            return time1+"秒前";

        } else if ((newTime - oldTime) / 1000 >= 60 && (newTime - oldTime) / 1000 / 60 < 60) {
            time1 = (int) ((newTime - oldTime) / 1000 / 60);
            return time1 +"分钟前";

        } else if ((newTime - oldTime) / 1000 / 60 >= 60 && (newTime - oldTime) / 1000 / 60 / 60 < 24) {
            time1 = (int) (newTime - oldTime) / 1000 / 60 / 60;
            return time1 + "小时前";
        } else if ((newTime - oldTime) / 1000 / 60 / 60 >= 24) {
            time1 = (int) (newTime - oldTime) / 1000 / 60 / 60 / 24;
            return time1 + "天前";
        }

        return "n天前";

    }


    @RequestMapping(value = "/delete", name = "用户留言删除")
    public String delete(String messageId) {
        messageService.delete(messageId);
        User user = userService.findById(getLoginUser().getId());
        Integer degree = user.getDegree();

        if (degree == 0){
            List<Message> messageList = messageService.findAll();
            request.setAttribute("messageList",messageList);

        }else if(degree == 1){
            List<Message> messageList = messageService.findByCompanyId(getCompanyId());
            request.setAttribute("messageList",messageList);
        }else{
            List<Message> messageList = messageService.findByAllMail(getLoginUser().getEmail());
            session.removeAttribute("count");
            request.setAttribute("messageList",messageList);
        }
        return "redirect:/myMessage.do";
    }

    @RequestMapping(value = "/myMessage", name = "我的留言")
    public String myMessage() {
        /*
         * 用户权限管理
         * */
        // 用户权限
        User user = userService.findById(getLoginUser().getId());
        Integer degree = user.getDegree();

        if (degree == 0){
            List<Message> messageList = messageService.findAll();
            request.setAttribute("messageList",messageList);

        }else if(degree == 1){
            List<Message> messageList = messageService.findByCompanyId(getCompanyId());
            request.setAttribute("messageList",messageList);
        }else{
            List<Message> messageList = messageService.findByAllMail(getLoginUser().getEmail());
            session.removeAttribute("count");
            for (Message message : messageList) {
                // 改变时间形式
                message.setReadState(1);
                messageService.update(message);
            }
            request.setAttribute("messageList",messageList);
        }



        return "/message/myMessage";
    }

    @RequestMapping(value = "/myNewMessage", name = "我的留言")
    public String myNewMessage() {

        User user = userService.findById(getLoginUser().getId());
        Integer degree = user.getDegree();

        if (degree == 0){
            List<Message> messageList = messageService.findAll();
            request.setAttribute("messageList",messageList);

        }else if(degree == 1){
            List<Message> messageList = messageService.findByCompanyId(getCompanyId());
            request.setAttribute("messageList",messageList);

        }else{

            List<Message> messageList = messageService.findByMail(getLoginUser().getEmail(),0);
            session.removeAttribute("count");

            for (Message message : messageList) {
                // 改变时间形式
                message.setReadState(1);
                messageService.update(message);
            }

            request.setAttribute("messageList",messageList);
        }



        return "/message/myMessage";
    }


    @RequestMapping(value = "/sendMessage", name = "发送留言")
    public String sendMessage(Message message) {
        // 封装实体

        // 消息id
        message.setMessageId(UUID.randomUUID().toString());
        message.setSendBy(getLoginUser().getId());
        message.setSendName(getLoginUser().getUserName());
        message.setSendTime(new Date());
        message.setCompanyId(getCompanyId());
        message.setCompanyName(getCompanyName());
        message.setReadState(0);

        messageService.save(message);

        return "message/message";
    }
}
